<?php
$loai = $lsp->getListLoaiSP();
$listchat = $chat->getListChatLieu();
$kg = $kieu -> getListKhoangGia();
$listKieu = $kieu->listkieudang_byloai(1);
if (isset($_POST['btnSumit'])) {
    $thanhcong = $sp->SanPham_Them($loi);
    if ($thanhcong == true) {
        header("location:?com=sp_list");
    }
}
?>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckfinder/ckfinder.js"></script>
<script>
    $(document).ready(function() {
        // ten kd
        $("input[name=ten_sp_vi]").blur(function() {
            var ten_sp = $(this).val();
            $.post("blocks/ajax.php", {str: ten_sp}, function(data) {
                $("input[name=ten_sp_kd_vi]").val(data);
            })
        });
        $("input[name=ten_sp_en]").blur(function() {
            var ten_sp = $(this).val();
            $.post("blocks/ajax.php", {str: ten_sp}, function(data) {
                $("input[name=ten_sp_kd_en]").val(data);
            })
        });
        //check all
        $('#tags_vi').on("keydown", function(event) {
            if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
                event.preventDefault();
            }
        }).autocomplete({
            source: function(request, response) {
                $.getJSON("ajax_tag_vi.php", {
                    term: extractLast(request.term)
                }, response);
            },
            search: function() {
                // custom minLength
                var term = extractLast(this.value);
                if (term.length < 2) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function(event, ui) {
                var terms = split(this.value);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push(ui.item.value);
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join("; ");
                return false;
            }
        });
        $('#tags_en').on("keydown", function(event) {
            if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
                event.preventDefault();
            }
        }).autocomplete({
            source: function(request, response) {
                $.getJSON("ajax_tag_en.php", {
                    term: extractLast(request.term)
                }, response);
            },
            search: function() {
                // custom minLength
                var term = extractLast(this.value);
                if (term.length < 2) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function(event, ui) {
                var terms = split(this.value);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push(ui.item.value);
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join("; ");
                return false;
            }
        });

        $("#color_all").click(function()
        {
            var checked_status = this.checked;
            $("input.mau").each(function()
            {
                this.checked = checked_status;
            });
        });
        $("#size_all").click(function()
        {
            var checked_status = this.checked;
            $("input.sizes").each(function()
            {
                this.checked = checked_status;
            });
        });

        $('#upload_images').ajaxForm({
            beforeSend: function() {
            },
            uploadProgress: function(event, position, total, percentComplete) {

            },
            complete: function(data) {
                var arrRes = JSON.parse(data.responseText);
                alert(arrRes['thongbao']);
                $("#hinhanh").html(arrRes['text'] + arrRes['str_hinhanh']);
                $("#div_upload").dialog('close');
            }
        });
        $("#btnUpload").click(function() {
            $("#div_upload").dialog({
                modal: true,
                title: 'Upload hình chữ nhật',
                width: 350,
                draggable: true,
                resizable: false,
                position: "center middle"
            });
        });
        $("#add_images").click(function() {
            $("#wrapper_input_files").append("<input type='file' name='images[]' /><br />");
        });
        
         $('#upload_images_2').ajaxForm({
            beforeSend: function() {
            },
            uploadProgress: function(event, position, total, percentComplete) {

            },
            complete: function(data) {
                var arrRes = JSON.parse(data.responseText);
                alert(arrRes['thongbao']);
                $("#hinhanh_2").html(arrRes['text'] + arrRes['str_hinhanh']);
                $("#div_upload_2").dialog('close');
            }
        });
        $("#btnUpload_2").click(function() {
            $("#div_upload_2").dialog({
                modal: true,
                title: 'Upload hình vuông',
                width: 350,
                draggable: true,
                resizable: false,
                position: "center middle"
            });
        });
        $("#add_images_2").click(function() {
            $("#wrapper_input_files_2").append("<input type='file' name='images[]' /><br />");
        });
        
    });
    function split(val) {
        return val.split(/;\s*/);
    }

    function extractLast(term) {
        return split(term).pop();
    }
</script>
<style>
#div_upload {
	font-size: 13px;
	text-align: center
}
</style>
<form action="" method="post" name="form_add_dm_ks">
  <div id="admin_navigation">
    <div style="float:left;width:90%">
      <h3>Quản lý sản phẩm: Thêm mới</h3>
    </div>
    <div style="float:left;width:5%;padding-top:5px">
      <input type="submit" class="save" name="btnSumit" value=""/>
      <br />
      <span>Save</span> </div>
    <div style="float:left;width:5%;padding-top:5px">
      <input type="reset" class="cancel" name="btnCancel" value=""/>
      <br />
      <span>Reset</span> </div>
    <div class="clr"></div>
  </div>
  <div id="main_admin">
    <div id="main_left" style="width:70%">
      <fieldset>
        <legend>Thông tin chung</legend>
        <table>
          <tr>
            <td>Loại SP</td>
            <td colspan="3"><select name="loai_id" id="loai_id" style="width: 200px">
                <?php while($row_loai = mysql_fetch_assoc($loai)){ ?>
                <option value="<?php echo $row_loai['loai_id'];?>"><?php echo $row_loai['loai_vi'];?></option>
                <?php } ?>
              </select></td>
          </tr>
          
          <tr>
            <td>Kiểu dáng</td>
            <td colspan="3"><table style="width:300px" cellpadding="4" cellspacing="0">
                <tr id="row_kieu">
                  <?php   while($row_kieu = mysql_fetch_assoc($listKieu)){
?>
                  <td style="width:30px;padding:10px;text-align:center;vertical-align: top;font-size: 12px;"><input class="size" type="checkbox" name="kieu_id[]" value="<?php echo $row_kieu['kieu_id']; ?>" />
                    <br />
                    <?php echo $row_kieu['kieu_vi']; ?></td>
                  <?php
} 
?>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td>Chất liệu</td>
            <td colspan="3"><table style="width:500px" cellpadding="4" cellspacing="0">
                <tr>
                  <?php
                                    while ($row_chat = mysql_fetch_assoc($listchat)) {
                                        ?>
                  <td style="width:20px;padding:4px;text-align:center;vertical-align: top;font-size: 12px;"><input class="size" type="checkbox" name="chat_id[]" value="<?php echo $row_chat['chat_id']; ?>" />
                    <br />
                    <?php echo $row_chat['chat_vi']; ?></td>
                  <?php } ?>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td>Mã SP</td>
            <td><input type="text" name="ma_sp" id="ma_sp" class="tf" value="<?php echo $_POST['ma_sp'] ?>" /></td>
          </tr>
          <tr>
            <td>Tên SP VI <img src="img/icons/vi.gif" vlign="center"/></td>
            <td><input type="text" name="ten_sp_vi" id="ten_sp_vi" class="tf" value="<?php echo $_POST['ten_sp_vi'] ?>" /></td>
            <td><img src="img/icons/en.gif" vlign="center"/></td>
            <td><input type="text" name="ten_sp_en" id="ten_sp_en" class="tf" value="<?php echo $_POST['ten_sp_en'] ?>" /></td>
          </tr>          
          <tr>
            <td>Tên SP KD <img src="img/icons/vi.gif" vlign="center"/></td>
            <td><input type="text" name="ten_sp_kd_vi" id="ten_sp_kd_vi" class="tf" value="<?php echo $_POST['ten_sp_kd_vi'] ?>"/></td>
            <td><img src="img/icons/en.gif" vlign="center"/></td>
            <td><input type="text" name="ten_sp_kd_en" id="ten_sp_kd_en" class="tf" value="<?php echo $_POST['ten_sp_kd_en'] ?>"/></td>
          </tr>
        
          <tr>
            <td>Khoảng giá</td>
            <td colspan="3"><select name="kg_id" id="kg_id" style="width: 200px">
                <?php while($row_kg = mysql_fetch_assoc($kg)){ 
                                    if($row_kg['giatu']==0) $str = "Dưới ".number_format($row_kg['giaden']);
                                    elseif($row_kg['giaden']==50000000) $str = "Trên ".number_format($row_kg['giatu']);
                                    else $str= number_format($row_kg['giatu'])." đến ".number_format($row_kg['giaden']);
                                    
                                    ?>
                <option value="<?php echo $row_kg['kg_id']; ?>"><?php echo $str; ?></option>
                <?php } ?>
              </select></td>
          </tr>
        </table>
      </fieldset>
    </div>
    <div id="main_right" style="width:30%">
      <fieldset class="details_form">
        <legend>Tags <img src="img/icons/vi.gif" vlign="center"/></legend>
        <table>
          <tr>
            <td colspan="2"><textarea id="tags_vi" style="width:300px;height:100px" class="meta" name="tags_vi" rows="10" ><?php echo $_POST['tags_vi'] ?></textarea></td>
          </tr>
        </table>
      </fieldset>
        <fieldset class="details_form">
        <legend>Tags <img src="img/icons/en.gif" vlign="center"/></legend>
        <table>
          <tr>
            <td colspan="2"><textarea id="tags_en" style="width:300px;height:100px" class="meta" name="tags_en" rows="10" ><?php echo $_POST['tags_en'] ?></textarea></td>
          </tr>
        </table>
      </fieldset>
    </div>
    <div id="main_details" style="clear:both;padding-top: 20px">
      <fieldset class="details_form">
        <legend>Thông tin chi tiết</legend>
        <table border="1" cellspacing="0">
          <tr>
            <td width="15%">Giá</td>
            <td><input type="text" name="gia" id="gia" class="tf" value="<?php echo $_POST['gia'] ?>" />
              <span class="error"><?php echo $loi[gia]; ?></span></td>
          </tr>
          <tr>
            <td width="15%">Khuyến mãi</td>
            <td><select name="khuyen_mai">
                <option value="0">--Chọn--</option>
                <?php for ($i = 5; $i < 100; $i = $i + 5) { ?>
                <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                <?php } ?>
              </select>
              % </td>
          </tr>
          <tr>
            <td width="15%">Ngày hết hạn</td>
            <td><input type="text" name="ngay" id="ngay" class="tf ngay"  />
              <span class="error"><?php echo $loi['ngay']; ?></span></td>
          </tr>
          <tr>
            <td>Upload hình - chữ nhật</td>
            <td><input type="button" id="btnUpload" value="Chọn hình" />
              <div id="hinhanh"></div></td>
          </tr>
          <tr>
            <td>Upload hình - vuông</td>
            <td><input type="button" id="btnUpload_2" value="Chọn hình" />
              <div id="hinhanh_2"></div></td>
          </tr>
          <tr>
            <td>Size</td>
            <td><input type="checkbox" id="size_all" name="size_all" value="all" />
              All size<br />
              <br />
              <table style="width:500px" cellpadding="0" cellspacing="0">
                <tr>
                  <?php
                                    $list_size = $size->getListSize();
                                    while ($row_size = mysql_fetch_assoc($list_size)) {
                                        ?>
                  <td style="width:10px;padding:0px"><input class="sizes" type="checkbox" name="size[]" value="<?php echo $row_size['size_id']; ?>" />
                    <br />
                    <?php echo $row_size['size']; ?></td>
                  <?php } ?>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td>Color</td>
            <td><input type="checkbox" id="color_all" name="color_all" value="all" />
              All color<br />
              <br />
              <table style="width:800px" cellpadding="0" cellspacing="0">
                <tr>
                  <?php
                                    $list_mau = $mau->getListMau();
                                    while ($row_mau = mysql_fetch_assoc($list_mau)) {
                                        ?>
                  <td style="width:20px;padding:0px;"><input class="mau" type="checkbox" name="mau[]" value="<?php echo $row_mau['mau_id']; ?>" />
                    <br />
                    <img  src="<?php echo "../" . str_replace('mausac', 'mausac/small', $row_mau['hinh']); ?>" /></td>
                  <?php } ?>
                </tr>
              </table></td>
          </tr>
        </table>
      </fieldset>
    </div>
    <div id="main_details" style="clear:both;padding-top: 20px">
      <fieldset class="details_form">
        <legend>++ Nội dung bài viết ++</legend>
        <table>
          <tr>
            <td>Mô tả <img src="img/icons/vi.gif" vlign="center" /></td>
            <td><textarea style="height:100px;width:550px" class="meta" name="mo_ta_vi" id="mo_ta_vi" rows="10"><?php echo $_POST['mo_ta_vi'] ?></textarea></td>
            <td><img src="img/icons/en.gif" vlign="center"/></td>
            <td><textarea style="height:100px;width:550px" class="meta" name="mo_ta_en" id="mo_ta_en" rows="10"><?php echo $_POST['mo_ta_en'] ?></textarea></td>
          </tr>
          <tr>
                <td>Chi tiết <img src="img/icons/vi.gif" vlign="center"/></td>
                <td>
                    <div style="width:550px;overflow: hidden">
                        <textarea class="meta" name="chi_tiet_vi" id="chi_tiet_vi"><?php echo $_POST['chi_tiet_vi'] ?></textarea>
                        <script type="text/javascript">
var editor = CKEDITOR.replace('chi_tiet_vi', {
uiColor: '#9AB8F3',
language: 'vi',
skin: 'office2003',
filebrowserImageBrowseUrl: 'ckfinder/ckfinder.html?Type=Images',
filebrowserFlashBrowseUrl: 'ckfinder/ckfinder.html?Type=Flash',
filebrowserImageUploadUrl: 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
filebrowserFlashUploadUrl: 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash',
toolbar: [
    ['Source', '-', 'Bold', 'Italic', 'Underline', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', 'Link', 'Unlink', 'Image', 'Styles', 'Format', 'TextColor', 'BGColor'],
]
});
                        </script>
                    </div>          
                </td>  
                <td><img src="img/icons/en.gif" vlign="center"/></td>
                <td>
                    <div style="width:550px;overflow: hidden">
                        <textarea class="meta" name="chi_tiet_en" id="chi_tiet_en"><?php echo $_POST['chi_tiet_en'] ?></textarea>
                        <script type="text/javascript">
var editor = CKEDITOR.replace('chi_tiet_en', {
uiColor: '#9AB8F3',
language: 'vi',
skin: 'office2003',
filebrowserImageBrowseUrl: 'ckfinder/ckfinder.html?Type=Images',
filebrowserFlashBrowseUrl: 'ckfinder/ckfinder.html?Type=Flash',
filebrowserImageUploadUrl: 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
filebrowserFlashUploadUrl: 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash',
toolbar: [
    ['Source', '-', 'Bold', 'Italic', 'Underline', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', 'Link', 'Unlink', 'Image', 'Styles', 'Format', 'TextColor', 'BGColor'],
]
});
                        </script>
                    </div>          
                </td>  
            </tr> 
        </table>
      </fieldset>
    </div>
    <div class="clr"></div>
  </div>
</form>
<div id="div_upload" style="display:none">
  <form id="upload_images" method="post" enctype="multipart/form-data" enctype="multipart/form-data" action="ajax.php">
  <div style="margin: auto;"> <img src="img/add.jpg" id="add_images" width="32" />
    <div id="wrapper_input_files">
      <input type="file" name="images[]" />
      <br />
    </div>
    <div class="clear"></div>
    <button style="margin-top: 10px;"class="button_colour" type="submit" id="btn_upload_images">
    <a href="#">Upload</a>
    </button>
  </div>
  </form>
</div>
<div id="div_upload_2" style="display:none">
  <form id="upload_images_2" method="post" enctype="multipart/form-data" enctype="multipart/form-data" action="ajax_2.php">
  <div style="margin: auto;"> <img src="img/add.jpg" id="add_images_2" width="32" />
    <div id="wrapper_input_files_2">
      <input type="file" name="images[]" />
      <br />
      <input type="file" name="images[]" />
      <br />
      <input type="file" name="images[]" />
      <br />
    </div>
    <div class="clear"></div>
    <button style="margin-top: 10px;"class="button_colour" type="submit" id="btn_upload_images_2">
    <a href="#">Upload</a>
    </button>
  </div>
  </form>
</div>

<script type="text/javascript" src="js/form.js"></script> 
<script type="text/javascript" src="js/jquery.ui.widget.js"></script> 
<script type="text/javascript" src="js/jquery.ui.datepicker.js"></script> 
<script type="text/javascript" src="js/jquery-ui.custom.js"></script> 
<script type="text/javascript" src="js/jquery.ui.dialog.js"></script>
<link rel="stylesheet" type="text/css" href="css/jquery.ui.base.css"/>
<link rel="stylesheet" type="text/css" href="css/jquery.ui.theme.css"/>
<script type="text/javascript">
    $(function(){     
        $('#loai_id').change(function(){
            var loai_id = $(this).val();
            $.post("ajax/getkieu.php", {loai_id: loai_id}, function(data) {
                $("tr#row_kieu").html(data);
            });
        });
        $(".ngay").datepicker({
            numberOfMonths: 1,  dateFormat: 'dd-mm-yy',
            monthNames: ['Một','Hai','Ba','Tư','Năm','Sáu','Bảy','Tám','Chín', 
            'Mười','Mười một','Mười hai'] ,
            monthNamesShort: ['Tháng1','Tháng2','Tháng3','Tháng4','Tháng5',
            'Tháng6','Tháng7','Tháng8','Tháng9','Tháng10','Tháng11','Tháng12'] ,
            dayNames: ['Chủ nhật', 'Thứ hai', 'Thứ ba', 'Thứ tư', 'Thứ năm',
            'Thứ sáu', 'Thứ bảy'],
            dayNamesMin: ['CN', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7'] ,
            showWeek: true , showOn: 'both',
            changeMonth: true , changeYear: true,
            currentText: 'Hôm nay', weekHeader: 'Tuần',
			minDate: 1,
            
        });
        
    })
    
</script>
<style>
    #div_upload_2 {
    font-size: 13px;
    text-align: center;
}
    
</style>
